<template>
  <div class="box">
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 标签页  g公司信息页面-->
      <!-- 标签页 -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="角色管理" name="second">
          <el-button type="primary" icon="el-icon-plus"   @click="isShow = true">新增角色</el-button>
          <!-- 表格 -->
          <!-- 表格 -->
          <el-table
            :data="tableData"
            style="width: 100%"
            border
            :default-sort="{ prop: 'date', order: 'descending' }"
          >
            <el-table-column label="序号" type="index"  />
            <el-table-column prop="name" label="角色" sortable width="180" />
            <el-table-column prop="description" sortable label="描述" />

            <el-table-column label="操作" >
              <!-- 通过 slot自定义结构  如果要获取到这一行的数据 可以通过 .row属性来获取 -->
              <template #default="{ row }">
                <el-button
                  type="text"
                  size="small"
                  @click="handleClick(row)"
                >分配权限</el-button>
                <el-button
                  type="text"
                  size="small"
                  @click="enterEdit(row)"
                >修改</el-button>
                  <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="onConfirm(row)"
                >
                  <el-button
                    slot="reference"
                    type="text"
                    size="small"
                  >删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            :current-page="currentPage4"
            :page-sizes="[1, 2, 4, 5]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息" name=" first">
          <comlnfo />
        </el-tab-pane>
      </el-tabs>

      <!-- 弹窗 -->
      <setting-dialog
        :visible="isShow"
      @update:visible='isShow=$event'
         @getData="getData"
      :form="form"
      @clear="clearHandler"
      >
    
      </setting-dialog>
    </el-card>
  </div>
</template>

<script>
import Comlnfo from './components/Comlnfo.vue'
import SettingDialog from './components/SettingDialog.vue'
// 导入接口
import { getRoles, getRoleById,delRoleById } from '@/api/setting'
export default {
  name: 'Setting',
  components: { Comlnfo, SettingDialog },
  data() {
    return {
        
      // 是否显示弹框
      isShow: false,
      currentPage4: 1,
      activeName: 'second',
      form: {
        name: '',
        description: ''
      },
      // 表格数据
      tableData: [],
      total: 1,
      page:1,
      pagesize:2
    }
  },
 created() {
   this.getData()
  },
 methods: {
  //  删除
  async onConfirm(row){
await delRoleById(row.id)
     // 判断是否为最后一条数据, 页码-1 除第一页
      if (this.tableData.length === 1 && this.page != 1) {
        this.page--
      }
        this.$message.success('删除成功,哦!')
      this.getData()

  },
    // 清空表单
    clearHandler () {
      this.form = {
        name: '',
        description: ''
      }
    },
    // 进入编辑
    async enterEdit (row) {
      // console.log('row:', row)
      const res = await getRoleById(row.id)
      // console.log('res:', res)
      // 设置给form
      this.form = res
      // 弹出dialog
      this.isShow = true
    },
    // 获取数据
    async getData () {
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })
      this.tableData = res.rows
      // 保存total
      this.total = res.total
    },
    // table内部点击
    async handleClick (row) {},
    // 页容量改变
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      // 一般会返回第一页
      this.page = 1
      // 重新获取数据
      this.getData()
    },
    // 页码改变
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getData()
    }
  }
}
</script>

<style lang="scss">
.box {
  width: 100%;
  .el-input__inner {
    width: 400px;
  }
  .el-form {
    padding-left: 100px;
  }
  .el-pagination {
    text-align: right;
    padding-right: 60px;

    input {
      width: 100px;
    }
  }
}
</style>
